package com.haipumi.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.haipumi.beans.haipumi_activity;
import com.haipumi.beans.haipumi_user;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IBackActivityDao;

public class BackActivityDaoImpl implements IBackActivityDao{

	DBHelper db=new DBHelper();
	@Override
	public List<haipumi_activity> findAllActivity() {
		String sql="select * from haipumi_activity ";
		List<haipumi_activity>activitylist=null;
		try {
			activitylist=db.finds(sql, null, haipumi_activity.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return activitylist;
	}

	@Override
	public int countAllActivity() {
		String sql="select count(*) from haipumi_activity ";
		int r=0;
		try {
			r=(int) db.findDouble(sql, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}

	@Override
	public haipumi_activity addActivity(haipumi_activity activity) {
		String sql=" insert into haipumi_activity (acname,acstatus,acdiscount,acstarttime,acendtime,acdescribe,acphoto) values (?,?,?,?,?,?,'yhj_10006.png') ";
		haipumi_activity backactivity=null;
		List<Object>params=new ArrayList<Object>();
		params.add(activity.getAcname());
		params.add(activity.getAcstatus());
		params.add(activity.getAcdiscount());
		params.add(activity.getAcstarttime());
		params.add(activity.getAcendtime());
		params.add(activity.getAcdescribe());
		int result=0;
		try {
			result=db.insertWithKey(sql, params);
			if(result>0){
				//成功
				backactivity=activity;
				backactivity.setAcid(Integer.toString(result));
				backactivity.setAcphoto("yhj_10006.png");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return backactivity;
	}

	@Override
	public haipumi_activity editActivity(haipumi_activity activity) {
		String sql=" update haipumi_activity set acname=?,acstatus=?,acdiscount=?,acstarttime=?,acendtime=?,acdescribe=? where acid=?";
		List<Object>params=new ArrayList<Object>();
		params.add(activity.getAcname());
		params.add(activity.getAcstatus());
		params.add(activity.getAcdiscount());
		params.add(activity.getAcstarttime());
		params.add(activity.getAcendtime());
		params.add(activity.getAcdescribe());
		params.add(activity.getAcid());
		int result=0;
		haipumi_activity  backactivity=null;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				backactivity=findActivity(activity.getAcid());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return backactivity;
	}

	@Override
	public haipumi_activity findActivity(String acid) {
		String sql="select * from haipumi_activity where acid=? ";
		List<Object>params=new ArrayList<Object>();
		params.add(acid);
		List<haipumi_activity>activitylist=null;
		haipumi_activity backactivity=null;
		try {
			activitylist=db.finds(sql, params, haipumi_activity.class);
			if(!activitylist.isEmpty()){
				backactivity=activitylist.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return backactivity;
	}

}
